package com.infore.authorityManagement.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.infore.authorityManagement.dto.Menu;
import com.infore.authorityManagement.entity.BusinessConstant;
import com.infore.authorityManagement.entity.SysPermission;
import com.infore.authorityManagement.enums.ResultStatus;
import com.infore.authorityManagement.service.ISysPermissionService;
import com.infore.authorityManagement.util.MenuTreeUtil;
import com.infore.authorityManagement.util.Page;
import com.infore.authorityManagement.util.ResultEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: PermissionController
 * @Description: 权限管理controller类
 * @author 谭红波
 * @date 2017年9月13日
 */
@Api(value = "权限管理", tags = "权限管理")
@Controller
@RequestMapping("/permissions")
public class PermissionController {
    @Autowired
    ISysPermissionService sysPermissionService;

    @ApiOperation(value = "查询权限", notes = "查询权限信息,约定：当pageSize小于1 或者 pageNo小于0或者其中一个为空时，不进行分页操作")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public @ResponseBody ResultEntity getPermissions(SysPermission sysPermission, Integer pageSize, Integer pageNo,
            String token) {
        if (pageSize == null || pageNo == null || pageSize < 1 || pageNo < 0) {
            List<SysPermission> list = sysPermissionService.getPermissions(sysPermission);
            return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setDataList(list);
        } else {
            Page<SysPermission> page = sysPermissionService.getPermissionsForPage(sysPermission, pageNo, pageSize);
            return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setPage(page);
        }
    }
    
    @ApiOperation(value = "查询菜单权限", notes = "查询权限信息,约定：当pageSize小于1 或者 pageNo小于0或者其中一个为空时，不进行分页操作")
    @RequestMapping(value = "/menus", method = RequestMethod.GET)
    public @ResponseBody ResultEntity getMenuPermissions(SysPermission sysPermission, Integer pageSize, Integer pageNo,
            String token) {
            List<SysPermission> list = sysPermissionService.getPermissions(sysPermission);
            List<Menu> menuList = new ArrayList<>();
        	for (SysPermission p : list) {
        		Menu menu = new Menu();
        		menu.setId(p.getPermissionId());
        		menu.setParentId(p.getPermissionParentID());
        		menu.setLevel(p.getPermissionLevel());
        		menu.setName(p.getPermissionName());
        		menu.setUrl(p.getUrl());
        		menu.setSeqNum(p.getSeqNum());
        		menu.setImg(p.getPermissionPictureUrl());
        		menu.setType(p.getPermissionType());
        		menu.setDescription(p.getDescription());
        		menu.setStatus(p.getStatus());
        		menuList.add(menu);
        	}
        	List<Menu> mlist = MenuTreeUtil.getTree(menuList);
            return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setDataList(mlist);
        
    }

    @ApiOperation(value = "查询权限", notes = "根据id查询权限信息")
    @RequestMapping(value = "/id", method = RequestMethod.GET)
    public @ResponseBody ResultEntity getPermissionById(@RequestParam String permissionId, String token) {
        SysPermission sysPermission = sysPermissionService.getPermissionById(permissionId);
        if (sysPermission == null) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PERMISSION_NOT_EXIST);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.QUERY_SUCCESS).setInfo(sysPermission);
    }

    @ApiOperation(value = "新增权限", notes = "新增权限信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody ResultEntity addPermission(SysPermission sysPermission, String token) {
        if (StringUtils.isBlank(sysPermission.getPermissionName()) || StringUtils.isBlank(sysPermission.getUrl())
                || sysPermission.getPermissionType() == null) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PARAM_ERROR);
        }
        Map<String, Object> map = sysPermissionService.addPermission(sysPermission);
        int resultIdt = (int) map.get("resultIdt");
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PERMISSION_EXIST);
        }
        if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
        }
        if (resultIdt == -3) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.APP_NOT_EXIST);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.INSERT_SUCCESS).setMapValue("permissionId", map.get("permissionId"));
    }

    @ApiOperation(value = "修改权限", notes = "修改权限信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public @ResponseBody ResultEntity updatePermission(SysPermission sysPermission, String token) {
        int resultIdt = sysPermissionService.updatePermission(sysPermission);
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.P_PERMISSION_NOT_EXIST);
        }
        if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PERMISSION_EXIST);
        }
        if (resultIdt == -3) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.UPDATE_SUCCESS);
    }

    @ApiOperation(value = "删除权限", notes = "根据id删除权限信息")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public @ResponseBody ResultEntity deletePermission(@RequestParam String permissionId, String token) {
        int resultIdt = sysPermissionService.deletePermission(permissionId);
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PERMISSION_NOT_EXIST);
        }
        if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.DELETE_SUCCESS);
    }
    
    @ApiOperation(value = "批量删除权限", notes = "根据id字符串删除权限信息")
    @RequestMapping(value = "/ids", method = RequestMethod.DELETE)
    public @ResponseBody ResultEntity deleteBatchPermission(@RequestParam String ids, String token) {
    	String[] idarr = ids.split(",");
    	for (int i = 0; i < idarr.length; i++) {
    		int resultIdt = sysPermissionService.deletePermission(idarr[i]);
            if (resultIdt == -1) {
                return new ResultEntity(ResultStatus.ERROR, BusinessConstant.PERMISSION_NOT_EXIST);
            }
            if (resultIdt == -2) {
                return new ResultEntity(ResultStatus.ERROR, BusinessConstant.DATA_ERROR);
            }
    	}
        
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.DELETE_SUCCESS);
    }

   
}
